package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.wheelview.NumericWheelAdapter;
import com.enqualcomm.kids.view.wheelview.OnWheelChangedListener;
import com.enqualcomm.kids.view.wheelview.WheelView;

import java.util.Arrays;
import java.util.List;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

/**
 * 编辑终端信息页面，生日对话框
 *
 * @author yzq
 */
public class MyDateTimePickerDialog extends Dialog implements
        View.OnClickListener {
    private static int START_YEAR = 1900, END_YEAR = 2020;
    private final OnDateTimeSetListener mCallBack;
    //	private final Calendar mCalendar;
    private int curr_year, curr_month, curr_day, curr_hour, curr_minute;
    // 添加大小月月份并将其转换为list,方便之后的判断
    String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
    String[] months_little = {"4", "6", "9", "11"};
    final WheelView wv_year, wv_month, wv_day;//, wv_hours, wv_mins;
    final List<String> list_big, list_little;

    private int windowWidth;
//    private String watchName;

//    public MyDateTimePickerDialog(Context context, OnDateTimeSetListener callBack, int year, int month, int day) {
//        this(context, START_YEAR, END_YEAR, callBack, year, month, day);
//    }

    public MyDateTimePickerDialog(Context context, OnDateTimeSetListener callBack, int year, int month, int day, String watchName) {
        this(context, START_YEAR, END_YEAR, callBack, year, month, day,watchName);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();


        lp.width = windowWidth - DensityUtil.dip2px(getContext(), 30); // 宽度
        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public MyDateTimePickerDialog(Context context, final int START_YEAR,
                                  final int END_YEAR, OnDateTimeSetListener callBack, int year, int month, int day,String watchName) {
        super(context);
        mCallBack = callBack;

        list_big = Arrays.asList(months_big);
        list_little = Arrays.asList(months_little);
        // 找到dialog的布局文件
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_layout, null);

        view.findViewById(R.id.sureBtn).setOnClickListener(this);
        view.findViewById(R.id.cancelBtn).setOnClickListener(this);

        TextView title = (TextView) view.findViewById(R.id.titleText);
        title.setText(getContext().getString(R.string.birthday));

        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        windowWidth = dm.widthPixels;
        int textSize = DensityUtil.adjustFontSize(windowWidth);
        // 年
        wv_year = (WheelView) view.findViewById(R.id.year);
        wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
        wv_year.setCyclic(true);// 可循环滚动
        wv_year.setLabel(getContext().getString(R.string.year));// 添加文字
        wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

        // 月
        wv_month = (WheelView) view.findViewById(R.id.month);
        wv_month.setAdapter(new NumericWheelAdapter(1, 12));
        wv_month.setCyclic(true);
        wv_month.setLabel(getContext().getString(R.string.month));
        wv_month.setCurrentItem(month);

        // 日
        wv_day = (WheelView) view.findViewById(R.id.day);
        wv_day.setCyclic(true);
        // 判断大小月及是否闰年,用来确定"日"的数据
        if (list_big.contains(String.valueOf(month + 1))) {
            wv_day.setAdapter(new NumericWheelAdapter(1, 31));
        } else if (list_little.contains(String.valueOf(month + 1))) {
            wv_day.setAdapter(new NumericWheelAdapter(1, 30));
        } else {
            // 闰年
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                wv_day.setAdapter(new NumericWheelAdapter(1, 29));
            else
                wv_day.setAdapter(new NumericWheelAdapter(1, 28));
        }
        wv_day.setLabel(getContext().getString(R.string.day));
        wv_day.setCurrentItem(day - 1);
        // 添加"年"监听
        OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                int year_num = newValue + START_YEAR;
                // 判断大小月及是否闰年,用来确定"日"的数据
                if (list_big.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                    wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                } else if (list_little.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                    wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                } else {
                    if ((year_num % 4 == 0 && year_num % 100 != 0)
                            || year_num % 400 == 0)
                        wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                    else
                        wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                }
            }
        };
        // 添加"月"监听
        OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                int month_num = newValue + 1;
                // 判断大小月及是否闰年,用来确定"日"的数据
                if (list_big.contains(String.valueOf(month_num))) {
                    wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                } else if (list_little.contains(String.valueOf(month_num))) {
                    wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                } else {
                    if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
                            .getCurrentItem() + START_YEAR) % 100 != 0)
                            || (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
                        wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                    else
                        wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                }
            }
        };
        wv_year.addChangingListener(wheelListener_year);
        wv_month.addChangingListener(wheelListener_month);
        wv_day.setTextSizeAndItemCount(textSize, 3);
//		wv_hours.TEXT_SIZE = textSize;
//		wv_mins.TEXT_SIZE = textSize;
        wv_month.setTextSizeAndItemCount(textSize, 3);
        wv_year.setTextSizeAndItemCount(textSize, 3);
        if (watchName != null) {
            MyFrontUtil.initTextViewText(context, (TextView) view.findViewById(R.id.titleText), watchName, context.getString(R.string.t_info_birthday));
        }
        setContentView(view);

    }

    public void onClick(View v) {
        if (v.getId() == R.id.cancelBtn) {
            dismiss();
        } else {
            curr_year = wv_year.getCurrentItem() + START_YEAR;
            curr_month = wv_month.getCurrentItem() + 1;
            curr_day = wv_day.getCurrentItem() + 1;
            if (mCallBack != null) {
                mCallBack.onDateTimeSet(curr_year, curr_month, curr_day, curr_hour, curr_minute);
            }
            dismiss();
        }

    }

    public interface OnDateTimeSetListener {
        void onDateTimeSet(int year, int monthOfYear, int dayOfMonth, int hour,
                           int minute);
    }

}

